package com.helloniu.wb.tools {

import com.helloniu.data.wb.RectStyle;
import com.helloniu.wb.CursorManager2;

import flash.display.Graphics;
import flash.events.MouseEvent;
import flash.geom.Point;
import flash.geom.Rectangle;

public class RectangularTool extends DrawingTool {
    //CONSTRUCTOR
    public function RectangularTool() {
        super();

        this.setWBStyle(new RectStyle(), false);
    }

	override public function getShapePoints2():Array {
    	var arr:Array = new Array();
    	
    	if(startP != null && endP != null) {
    		arr.push(startP);
    		arr.push(endP);
    	}    	

    	return arr;
    }

    override public function setShapePoints(arr:Array):void {
        super.setShapePoints(arr);

        resetStartEndPoint();
    }

    override public function setEndPoint(aendP:Point, appendEnd:Boolean = true):void {
        shapeRawData[1] = aendP;

        // @temp tonyy
        resetStartEndPoint();
    }

    //OVERRIDDEN FUNCTIONS
    override protected function createChildren():void {
        super.createChildren();

        resizeBox = new RectangleResizeBox();
        this.addChild(resizeBox);
        resizeBox.addEventListener(MouseEvent.MOUSE_DOWN, resizeMouseDownHandler);
    }

    public function resizeMouseDownHandler(event:MouseEvent):void {
        //stop propogation so the BlankPaperWrapper doesn't listen and
        //act on the mouseDown event
        event.stopPropagation();

        //adjust our move x and y's
        moveStartX = event.target.parent.contentMouseX;
        moveStartY = event.target.parent.contentMouseY;

        moveEndX = event.target.parent.contentMouseX;
        moveEndY = event.target.parent.contentMouseY;
        moveDistanceX = 0;
        moveDistanceY = 0;

        startDragging(event);
    }

    override protected function resetMoveVars():void {
        var resizeType:Number = resizeBox.getResizeType();

        if(ResizeManager.isResizeBoxVisible() && resizeType == -1) {
        	return;
        }

        if(resizeType == 0) {
            addStartXY(moveDistanceX, moveDistanceY);
        }
        else if(resizeType == 1) {
            addStartXY(0, moveDistanceY);
        }
        else if(resizeType == 2) {
            addStartXY(0, moveDistanceY);
            addEndXY(moveDistanceX, 0);
        }
        else if(resizeType == 3) {
            addStartXY(moveDistanceX, 0);
        }
        else if(resizeType == 4) {
            addEndXY(moveDistanceX, 0);
        }
        else if(resizeType == 5) {
            addStartXY(moveDistanceX, 0);
            addEndXY(0, moveDistanceY);
        }
        else if(resizeType == 6) {
            addEndXY(0, moveDistanceY);
        }
        else if(resizeType == 7) {
            addEndXY(moveDistanceX, moveDistanceY);
        }
        else {
              addStartXY(moveDistanceX, moveDistanceY);
              addEndXY(moveDistanceX, moveDistanceY);
        }

        //Tool.traceT("--move-or-modify---rect: " +  isDragging +"|"+ isDragging2);
    }

    private function resetStartEndPoint():void {
        var shapeDots:Array = getShapePoints();
        var sp:Point = shapeDots[0] as Point;
        var ep:Point = shapeDots[1] as Point;

        var spX:Number = sp.x;
        var spY:Number = sp.y;
        var epX:Number = ep.x;
        var epY:Number = ep.y;

        startP = new Point(Math.min(spX, epX), Math.min(spY, epY));
        endP = new Point(Math.max(spX, epX), Math.max(spY, epY));

        //setStartPoint(startP, false);
        //_dots[1] = endP;
    }

    override public function addStartXY(addx:Number, addy:Number):void {
        var sp:Point = startP;
        var spX:Number = sp.x + addx;
        var spY:Number = sp.y + addy;

        startP = new Point(spX, spY);
        //setStartPoint(startP, false);
    }

    override public function addEndXY(addx:Number, addy:Number):void {
        var ep:Point = endP;
        var epX:Number = ep.x + addx;
        var epY:Number = ep.y + addy;

        endP = new Point(epX, epY);
        //_dots[1] = endP;
    }

    override protected function repaint():void {
    	super.repaint();
        updateDisplayList(0, 0);
    }

    override protected function updateDisplayList(unscaledW:Number, unscaledH:Number):void {
        if(getShapePoints().length < 2) {
            return;
        }

        drawRectangularShape();
        // @temp tonyy
        updateResizeHandle();
    }

    protected function calculateRect():Rectangle {
    	var w:Number;
        var h:Number;

        if(getShapePoints().length < 2) {
            return null;
        }

		// 怎么从shapeInfo里获得这个 getShapePoints2
        var shapeDots:Array = getShapePoints2();

        if(shapeDots.length == 0) {
        	shapeDots = getShapePoints();
        	
        	// @by tonyy, fix bug-2009-07-31-4
        	// reset start point, end point
        	resetStartEndPoint();
        }

        var sp:Point = shapeDots[0] as Point;
        var ep:Point = shapeDots[1] as Point;

        //var sp:Point = startP;
        //var ep:Point = endP;

        var spX:Number = sp.x;
        var spY:Number = sp.y;
        var epX:Number = ep.x;
        var epY:Number = ep.y;

        if(Point.distance(sp, ep) < 20) {
            // @temp tonyy
            //return;
        }

        if(!isNaN(epX)) {
            w = epX - spX;
            h = epY - spY;

            w = Math.abs(w); //switch to positive number;
            h = Math.abs(h); //switch to positive number;

            //WE NEED TO OVER RIDE THE WIDTH AND HEIGHT IF THEY ARE DRAGGING AND UP/lEFT OF THE STARTX OR STARTY...
            //if mouse is top left of startX then set both w/h to the MINIMUM
            //get our ending mouse position by using endX, and endY
            if(epX < spX && epY < spY) {
                w = MINIMUM_WIDTH;
                h = MINIMUM_HEIGHT;
            }

            //if the mouse is left of starting position, but dragging below it, start to expand then box downward (only set the MINIMUM WIDTH, not the height
            if(epX < spX && epY > spY) {
                w = MINIMUM_WIDTH;
            }

            //if the mouse is right of the starting position, and draggin above it, expand the box to the right (only set the min height)
            if(epX > spX && epY < spY) {
                h = MINIMUM_HEIGHT;
            }
        }

        return new Rectangle(spX, spY, w, h);
    }

    protected function updateResizeHandle():void {
    	var rect:Rectangle = calculateRect();

        if(CursorManager2.getToolType() == CursorManager2.MODIFY_TOOL) {
            resizeBox.setStartPoint(new Point(rect.x, rect.y));
            resizeBox.setWidth(rect.width);
            resizeBox.setHeight(rect.height);

            //resizeBox.visible = true;
            ResizeManager.setResizeBox(resizeBox);
            resizeBox.invalidateDisplayList();
        }
        else {
            //resizeBox.visible = false;
            ResizeManager.setResizeBox(resizeBox, false);
        }
    }

    override protected function isDTOutofWB():Boolean {
    	if(super.isDTOutofWB()) {
        	return true;
        }

    	var prect:Rectangle = new Rectangle(0, 0, this.parent.width, this.parent.height);
    	var rect:Rectangle = calculateRect();

    	if(rect != null && !prect.containsRect(rect)) {
        	return true;
        }

    	return false;
    }

    protected function drawRectangularShape():void {
    }

    //2 is enough to at least see something is happening on the screen, if we set it to 0, you wouldnt see anything.
    protected static var MINIMUM_WIDTH:Number = 2;
    protected static var MINIMUM_HEIGHT:Number = 2;
    private var resizeBox:RectangleResizeBox;
    protected var startP:Point = null;
    protected var endP:Point = null;
}
}